package com.shengde.jinwawaforleader.model.myJob.monitoring;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProGetMonitoringAccesstoken;
import com.shengde.jinwawaforleader.network.protocolbean.ProMonitoringList;
import com.videogo.constant.IntentConsts;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZCameraInfo;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 实时视频
 * Created by Administrator on 2016/9/1 0001.
 */
public class MonitoringVideoActivity extends BaseActivity {
	private static final int FINSH = 1;
	private static final String TAG = "MonitoringVideoActivity";
	private static final int EXIST = 2;
	private TextView tv_back;
	private TextView tv_title;
	private ListView lv_monitoring;  //监控列表
	private CommonBaseAdapter monitoringVideoAdapter;
	private List<ProMonitoringList.CameraListBean> cameraList_list;
	private String accessToken; //获取的token
	private EZOpenSDK mEZOpenSDK = null;
	EZCameraInfo cameraInfo;
	private String start_t;  //开始时间的赋值
	private String start_w;  //开始星期的赋值
	private String end_t;//结束时间的赋值
	private String end_w;//结束星期的赋值

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FINSH:
					if (monitoringVideoAdapter == null) {
						monitoringVideoAdapter = new CommonBaseAdapter(MonitoringVideoActivity.this, cameraList_list, R.layout.monitoring_list_item) {
							@Override
							public void convert(ViewHolder holder, final int position) {
								Log.i(TAG, "convert: deviceSerial" + cameraList_list.get(position).getDeviceSerial());
								holder.setText(R.id.tv_deviceName, cameraList_list.get(position).getDeviceName());
								int start_time = cameraList_list.get(position).getStartTime();
								int start_week = cameraList_list.get(position).getStartWeek();
								int end_time = cameraList_list.get(position).getEndTime();
								int end_week = cameraList_list.get(position).getEndWeek();
								if (start_time>=0&&start_time<=9){
									start_t ="0"+start_time+":00";
								}else {
									start_t=start_time+":00";
								}
								if (end_time>=0&&end_time<=9){
									end_t ="0"+end_time+":00";
								}else {
									end_t=end_time+":00";
								}

								String [] weekdays = {"星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
								start_w = weekdays[start_week];
								end_w = weekdays[end_week];
								holder.setText(R.id.tv_open_week_time, start_w + "～" + end_w);
								holder.setText(R.id.tv_open_day_time, start_t + "～" + end_t);
								if (cameraList_list.get(position).getIsShow() == 1) {
									return;
								}
								holder.getView(R.id.btn_bianji).setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										Intent intent = new Intent(MonitoringVideoActivity.this, MonitoringAccreditActivity.class);
										intent.putExtra("deviceName", cameraList_list.get(position).getDeviceName());
										intent.putExtra("deviceSerial", cameraList_list.get(position).getDeviceSerial());
										intent.putExtra("start_time", cameraList_list.get(position).getStartTime());
										intent.putExtra("end_time", cameraList_list.get(position).getEndTime());
										intent.putExtra("start_week", cameraList_list.get(position).getStartWeek());
										intent.putExtra("end_week", cameraList_list.get(position).getEndWeek());
										startActivity(intent);
									}
								});
								holder.getView(R.id.item_play_btn).setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										Toast.makeText(MonitoringVideoActivity.this, getString(R.string.video_loading), Toast.LENGTH_SHORT).show();
										new Thread() {
											@Override
											public void run() {
												super.run();
												try {
													cameraInfo = mEZOpenSDK.getCameraInfo(cameraList_list.get(position).getDeviceSerial());
													Intent intent = new Intent(MonitoringVideoActivity.this, EZRealPlayActivity.class);
													intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
													intent.putExtra("deviceName", cameraList_list.get(position).getDeviceName());
													startActivity(intent);
												} catch (BaseException e) {
													e.printStackTrace();
												}
											}
										}.start();
									}
								});

							}
						};
						lv_monitoring.setAdapter(monitoringVideoAdapter);
					} else {
						monitoringVideoAdapter.notifyDataSetChanged();
					}
					break;
				case EXIST:
					mEZOpenSDK.setAccessToken(accessToken);
					Log.i(TAG, "handleMessage: accessToken" + accessToken);
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.monitoring_video_activity);
	}

	@Override
	public void initView() {

		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		lv_monitoring = (ListView) findViewById(R.id.lv_monitoring);
	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.monitoring_video));
		mEZOpenSDK = EZOpenSDK.getInstance();
		showMonitoringList();
		showMonitoringToken(EXIST);
	}


	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}

	/**
	 * 显示监控列表
	 */
	private void showMonitoringList() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/camera/cameraSchool";
		} else {
			url = BaseProtocol.HOST_BASE + "/camera/cameraSchool";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("schoolid", User.getSchoolId(this));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProMonitoringList pro_monitoring_list = new Gson().fromJson(result, ProMonitoringList.class);
				if (pro_monitoring_list != null && pro_monitoring_list.getCode() == 0) {
					cameraList_list = pro_monitoring_list.getCameraList();
					mHandler.sendEmptyMessage(FINSH);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}


	/**
	 * 获取accessToken 的方法
	 */
	private void showMonitoringToken(final int flag) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/camera/getAccessToken";
		} else {
			url = BaseProtocol.HOST_BASE + "/camera/getAccessToken";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProGetMonitoringAccesstoken pro_access_token = new Gson().fromJson(result, ProGetMonitoringAccesstoken.class);
				if (pro_access_token != null && pro_access_token.getCode() == 0) {
					accessToken = pro_access_token.getAccessToken();
					mHandler.sendEmptyMessage(flag);
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
